﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using Sharp3DMath.Core;
using Sharp3DMath.Geometry2D;

namespace Sharp3DMath.Strokes
{
    public class StrokeGeometry
    {
        /// <summary>
        /// 求笔画上离某点距离最近的点,以及距离
        /// </summary>
        /// <param name="p"></param>
        /// <param name="stroke"></param>
        /// <param name="dis"></param>
        /// <returns></returns>
        public static Vector2F NearestPointToStroke(Vector2F p,Vector2F[] stroke,ref double dis)
        {   //Todo UnitTest
            double distance = 10000.0;
            Vector2F pe = Vector2F.Zero;
            foreach (Vector2F item in stroke)
            {
                double dt = DistanceMethods.Distance(item, p);
                if (dt < distance)
                {
                    distance = dt;
                    pe = item;
                }
            }
            dis = distance;
            return pe;
        }

        
        /// <summary>
        /// 求笔画中某点到其他点的最大距离
        /// </summary>
        /// <param name="p"></param>
        /// <param name="stroke"></param>
        /// <param name="maxindex"></param>
        /// <returns></returns>
        private double MaxDistance_PointToOthers_InStroke(Vector2F p, Vector2F[] stroke,ref int maxPointIndex)
        {
            double mdistance = 0.0,tdistance = 0.0;
            int mi = 0;
            for (int i = 0; i < stroke.Length; i++)
            {
                tdistance = DistanceMethods.Distance(p, stroke[i]);
                if (tdistance > mdistance)
                {
                    mdistance = tdistance;
                    mi = i;
                }
            }
            maxPointIndex = mi;
            return mdistance;   
        }

        /// <summary>
        /// 求出笔画中距离最大的两个点
        /// 效率低
        /// Todo 效率低
        /// </summary>
        /// <param name="stroke"></param>
        /// <param name="point1"></param>
        /// <param name="point2"></param>
        private void MaxDistance_2Points_InStroke(Vector2F[] stroke, ref Vector2F point1, ref Vector2F point2)
        {
            double maxdistance = 0.0;
            int IndexOfPoint1 = 0, IndexOfPoint2 = 0;
            double distance = 0.0;
            int id = 0;
            for (int i = 0; i < stroke.Length; i++)
            {
                distance = MaxDistance_PointToOthers_InStroke(stroke[i], stroke, ref id);
                if (distance > maxdistance)
                {
                    IndexOfPoint1 = i;
                    IndexOfPoint2 = id;
                    maxdistance = distance;
                }
            }
            point1 = stroke[IndexOfPoint1];
            point2 = stroke[IndexOfPoint2];
        }

    }
}
